﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace NetTopologySuite.Noding
{
	public class SimpleSegmentSetMutualIntersector : ISegmentSetMutualIntersector
	{
		public SimpleSegmentSetMutualIntersector(ICollection<ISegmentString> baseSegStrings)
		{
			this._baseBaseSegStrings = baseSegStrings;
		}

		public void Process(ICollection<ISegmentString> segmentStrings, ISegmentIntersector segmentIntersector)
		{
			foreach (ISegmentString ss in this._baseBaseSegStrings)
			{
				foreach (ISegmentString ss2 in segmentStrings)
				{
					SimpleSegmentSetMutualIntersector.Intersect(ss, ss2, segmentIntersector);
					if (segmentIntersector.IsDone)
					{
						return;
					}
				}
			}
		}

		private static void Intersect(ISegmentString ss0, ISegmentString ss1, ISegmentIntersector segInt)
		{
			Coordinate[] coordinates = ss0.Coordinates;
			Coordinate[] coordinates2 = ss1.Coordinates;
			for (int i = 0; i < coordinates.Length - 1; i++)
			{
				for (int j = 0; j < coordinates2.Length - 1; j++)
				{
					segInt.ProcessIntersections(ss0, i, ss1, j);
					if (segInt.IsDone)
					{
						return;
					}
				}
			}
		}

		private readonly ICollection<ISegmentString> _baseBaseSegStrings;
	}
}
